from django.db import models
from django.contrib.auth.models import User, AbstractUser
from django.utils import timezone


# 上传指定文件夹
def get_business_upload_to(instance, filename):
    """
    A simple way to return the full path
    room_image
    background_image
    finished_product
    """
    rpath = 'business_image/' + instance.username + '/' + filename
    return rpath


def get_user_upload_to(instance, filename):
    """
    A simple way to return the full path
    room_image
    background_image
    finished_product
    """
    rpath = 'user_image/' + instance.username + '/' + filename
    return rpath


class UserInfo(User):
    '''用户表'''
    account = models.CharField(max_length=32, blank=True)
    imgSrc = models.ImageField(upload_to=get_user_upload_to, blank=True)
    phone = models.CharField(max_length=11, blank=True)
    province = models.CharField(max_length=12, blank=True)
    city = models.CharField(max_length=12, blank=True)
    area = models.CharField(max_length=12, blank=True)
    address = models.CharField(max_length=200, blank=True)

    def __str__(self):
        return self.username

    class Meta:
        verbose_name = '用户表'
        verbose_name_plural = '用户表'
        ordering = ['id']


class BusinessType(models.Model):
    category = models.CharField(max_length=32)
    uploadTime = models.DateTimeField(auto_now_add=True)
    uploadUser = models.ForeignKey(to=User, on_delete=models.CASCADE, to_field='id')
    lastChangeTime = models.DateTimeField(auto_now=True)
    description = models.CharField(max_length=300, blank=True, default='')

    def __str__(self):
        return self.category

    class Meta:
        verbose_name = '商户类别表'
        verbose_name_plural = '商户类别表'
        ordering = ['id']


class BusinessInfo(User):
    '''商户表'''
    account = models.CharField(max_length=32, blank=True)
    businessType = models.ForeignKey(to='BusinessType', on_delete=models.CASCADE, to_field='id')
    imgSrc = models.ImageField(upload_to=get_business_upload_to)
    phone = models.CharField(max_length=11, blank=True)
    province = models.CharField(max_length=12, blank=True)
    city = models.CharField(max_length=12, blank=True)
    area = models.CharField(max_length=12, blank=True)
    address = models.CharField(max_length=200, blank=True)

    def __str__(self):
        return self.username

    class Meta:
        verbose_name = '商户表'
        verbose_name_plural = '商户表'
        ordering = ['id']


class GlobalSetting(models.Model):
    # 用户审核
    userIsCheckOpen = models.BooleanField(default=True)
    # 商户审核
    businessIsCheckOpen = models.BooleanField(default=True)

    # trialDays = models.CharField(max_length=32, default='3')
    # userOnlineLimit = models.CharField(max_length=32, default='2')
    # uploadLimit = models.CharField(max_length=32, default='300')

    class Meta:
        verbose_name = '全局设置'
        verbose_name_plural = '全局设置'
        ordering = ['id']
